package com.sgb.kjwl.view.widget.pick;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.R;
import com.sgb.kjwl.callback.AitContactDecoration;
import com.sgb.kjwl.callback.ItemType;
import com.sgb.kjwl.callback.OnItemClickListener;
import com.sgb.kjwl.databinding.LayoutTenderRequestResponseBinding;
import com.sgb.kjwl.model.entity.pick.EnterpriseBiddingDetailEntity5;
import com.sgb.kjwl.view.ui.adapter.pick.PickEnterpriseMultiItemAdapter;
import com.swgk.core.base.CallBack;

import java.util.ArrayList;
import java.util.List;

/**
 * 招采报名响应布局
 */
public class PickTenderRequestResponseView extends LinearLayout implements View.OnClickListener {

    private Context mContext;
    private LayoutTenderRequestResponseBinding tenderRequestResponseView;
    private ArrayList<EnterpriseBiddingDetailEntity5.TenderingAsksEntity> items = new ArrayList<>();
    private PickEnterpriseMultiItemAdapter adapter; //响应
    private EnterpriseBiddingDetailEntity5.TenderingAsksEntity tenderingAsksEntity;
    private int tenderingAsksEntityPos;
    private boolean isallSelect = false; //是否全部响应 默认不响应
    private boolean isExpandable = false;  //是否全部展开 默认收起

    public PickTenderRequestResponseView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    private void init() {
        tenderRequestResponseView = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.layout_tender_request_response, this, true);
        tenderRequestResponseView.tvAllIsOpen.setOnClickListener(this);
        initView();
    }

    private void initView() {
        //取得设置好的drawable对象
        Drawable drawable = getResources().getDrawable(R.drawable.check_bg_select);
        //设置drawable对象的大小
        drawable.setBounds(0, 0, 60, 60);
        //设置CheckBox对象的位置，对应为左、上、右、下
        tenderRequestResponseView.checkBoxSelect.setCompoundDrawables(drawable, null, null, null);
        tenderRequestResponseView.checkBoxSelect.setOnClickListener(v -> {
            boolean isSelect = !isallSelect;
            for (int i = 0; i < adapter.getmList().size(); i++) {
                if (isSelect != adapter.getHolder(i).ivSelect.isChecked()) {
                    adapter.getHolder(i).ivSelect.setChecked(isSelect);
                }
            }
        });

        tenderRequestResponseView.recyclerView5.setLayoutManager(new LinearLayoutManager(mContext));
        items = new ArrayList<>();
        adapter = new PickEnterpriseMultiItemAdapter(mContext, items, new CallBack() {
            @Override
            public void onSuccess(int i, String s, int i1, Object o, String s1) {
                if ("选中".equals(s)) {
                    setAllSelect();
                } else {
                    setIsOpen();
                }
            }

            @Override
            public void onFailed(int i, int i1, String s) {
            }
        });
        adapter.setOnItemClickListener(new OnItemClickListener<EnterpriseBiddingDetailEntity5.TenderingAsksEntity>() {
            @Override
            public void onClick(View v, int pos, EnterpriseBiddingDetailEntity5.TenderingAsksEntity data) {
                tenderingAsksEntity = data;
                tenderingAsksEntityPos = pos;
            }

            @Override
            public boolean onLongClick(View v, int pos, EnterpriseBiddingDetailEntity5.TenderingAsksEntity data) {
                return false;
            }
        });
        tenderRequestResponseView.recyclerView5.setAdapter(adapter);

        List<Integer> noDividerViewTypes = new ArrayList<>(1);
        noDividerViewTypes.add(ItemType.SIMPLE_LABEL);
        tenderRequestResponseView.recyclerView5.addItemDecoration(new AitContactDecoration(mContext, LinearLayoutManager.VERTICAL, noDividerViewTypes));
    }

    /**
     * 同步左边选中按钮
     */
    private void setAllSelect() {
        int pos = 0;
        for (EnterpriseBiddingDetailEntity5.TenderingAsksEntity tenderingAsksEntity : items) {
            if (tenderingAsksEntity.getIsResponse() == 2) {
                pos++;
            }
        }
        if (pos == items.size()) {
            isallSelect = true;
            tenderRequestResponseView.checkBoxSelect.setChecked(true);
        } else {
            isallSelect = false;
            tenderRequestResponseView.checkBoxSelect.setChecked(false);
        }
    }

    /**
     * 同步右边展开收起按钮
     */
    private void setIsOpen() {
        int pos = 0;
        for (EnterpriseBiddingDetailEntity5.TenderingAsksEntity tenderingAsksEntity : adapter.getmList()) {
            if (tenderingAsksEntity.isExpandable()) {
                pos++;
            }
        }
        if (pos == items.size()) {
            isExpandable = true;
            tenderRequestResponseView.tvAllIsOpen.setText("全部折叠");
        } else if (pos == 0) {
            isExpandable = false;
            tenderRequestResponseView.tvAllIsOpen.setText("全部展开");
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_all_is_open:
                for (int i = 0; i < adapter.getmList().size(); i++) {
                    if (isExpandable) {
                        if (adapter.getmList().get(i).isExpandable()) {
                            int pos = adapter.getHolder(i).pos;
                            if (pos == adapter.getHolder(i).getAdapterPosition()) {
                                adapter.getHolder(i).pos = -1;
                                adapter.getmList().get(i).setExpandable(false);
                                adapter.getHolder(i).keepOne.toggle(adapter.getHolder(i), adapter.getHolder(i).ivTypeOpen, 0);
                            }
                        }
                    } else {
                        if (!adapter.getmList().get(i).isExpandable()) {
                            int pos = adapter.getHolder(i).pos;
                            if (pos != adapter.getHolder(i).getAdapterPosition()) {
                                adapter.getHolder(i).pos = adapter.getHolder(i).getAdapterPosition();
                                adapter.getmList().get(i).setExpandable(true);
                                adapter.getHolder(i).keepOne.toggle(adapter.getHolder(i), adapter.getHolder(i).ivTypeOpen, 0);
                            }
                        }
                    }
                }
                setIsOpen();
                break;
            default:
                break;
        }
    }

    /**
     * 数据更新
     *
     * @param urls
     */
    public void setData(String urls) {
        if (!urls.equals("") && tenderingAsksEntity != null) {
            items.get(tenderingAsksEntityPos).setFileUrl(urls);
            adapter.notifyItemChanged(tenderingAsksEntityPos);
        } else {
            tenderingAsksEntity.setFileUrl(null);
        }
    }

    /**
     * 数据初始化
     *
     * @param tenderingAsks
     */
    public void setNewData(List<EnterpriseBiddingDetailEntity5.TenderingAsksEntity> tenderingAsks) {
        items.clear();
        adapter.setNewData(tenderingAsks);
        setAllSelect();
        setIsOpen();
    }

    /**
     * 获取当前数据的条数
     *
     * @return
     */
    private int getItemCount() {
        return adapter.getItemCount();
    }

    /**
     * 控制当前ui布局的显示和隐藏
     */
    public void setView() {
        if (getItemCount() == 0) {
            tenderRequestResponseView.emptyView.setVisibility(View.VISIBLE);
            tenderRequestResponseView.rootLayout.setVisibility(GONE);
        } else {
            tenderRequestResponseView.rootLayout.setVisibility(View.VISIBLE);
            tenderRequestResponseView.emptyView.setVisibility(View.GONE);
        }
    }

    /**
     * 控制当前ui布局的显示和隐藏
     */
    public void setView(int type) {
        if (getItemCount() == 0) {
            tenderRequestResponseView.emptyView.setVisibility(type);
            tenderRequestResponseView.rootLayout.setVisibility(GONE);
        } else {
            tenderRequestResponseView.rootLayout.setVisibility(type);
            tenderRequestResponseView.emptyView.setVisibility(View.GONE);
        }
    }

    /**
     * 判断当前布局是否全部响应，全部响应ture，否则 false
     * @return
     */
    public boolean isNext() {
        if (adapter != null && adapter.getmList() != null) {
            if (!isallSelect) {
                return false;
            }
        }
        return true;
    }
}
